/**
 * @class common.ueditor
 * @extends common.base.object
 * ueditor封装类
 */
define(function (require) {
    var EDITOR_DEBUG = false;
    var BaseObject = require('common/base/object'),
        src_files = [
            '../ueditor.config.js',
            'editor.js',
            'core/browser.js',
            'core/utils.js',
            'core/EventBase.js',
            'core/dtd.js',
            'core/domUtils.js',
            'core/Range.js',
            'core/Selection.js',
            'core/Editor.js',
            'core/Editor.defaultoptions.js',
            'core/loadconfig.js',
            'core/ajax.js',
            'core/filterword.js',
            'core/node.js',
            'core/htmlparser.js',
            'core/filternode.js',
            'core/plugin.js',
            'core/keymap.js',
            'core/localstorage.js',
            'plugins/defaultfilter.js',
            'plugins/inserthtml.js',
            'plugins/autotypeset.js',
            'plugins/autosubmit.js',
            'plugins/background.js',
            'plugins/image.js',
            'plugins/justify.js',
            'plugins/font.js',
            'plugins/link.js',
            'plugins/iframe.js',
            'plugins/scrawl.js',
            'plugins/removeformat.js',
            'plugins/blockquote.js',
            'plugins/convertcase.js',
            'plugins/indent.js',
            'plugins/print.js',
            'plugins/preview.js',
            'plugins/selectall.js',
            'plugins/paragraph.js',
            'plugins/directionality.js',
            'plugins/horizontal.js',
            'plugins/time.js',
            'plugins/rowspacing.js',
            'plugins/lineheight.js',
            'plugins/insertcode.js',
            'plugins/cleardoc.js',
            'plugins/anchor.js',
            'plugins/wordcount.js',
            'plugins/pagebreak.js',
            'plugins/wordimage.js',
            'plugins/dragdrop.js',
            'plugins/undo.js',
            'plugins/copy.js',
            'plugins/paste.js',
            'plugins/puretxtpaste.js',
            'plugins/list.js',
            'plugins/source.js',
            'plugins/enterkey.js',
            'plugins/keystrokes.js',
            'plugins/fiximgclick.js',
            'plugins/autolink.js',
            'plugins/autoheight.js',
            'plugins/autofloat.js',
            'plugins/video.js',
            'plugins/table.core.js',
            'plugins/table.cmds.js',
            'plugins/table.action.js',
            'plugins/table.sort.js',
            'plugins/contextmenu.js',
            'plugins/shortcutmenu.js',
            'plugins/basestyle.js',
            'plugins/elementpath.js',
            'plugins/formatmatch.js',
            'plugins/searchreplace.js',
            'plugins/customstyle.js',
            'plugins/catchremoteimage.js',
            'plugins/snapscreen.js',
            'plugins/insertparagraph.js',
            'plugins/webapp.js',
            'plugins/template.js',
            'plugins/music.js',
            'plugins/autoupload.js',
            'plugins/autosave.js',
            'plugins/charts.js',
            'plugins/section.js',
            'plugins/simpleupload.js',
            'plugins/serverparam.js',
            'plugins/insertfile.js',
            'ui/ui.js',
            'ui/uiutils.js',
            'ui/uibase.js',
            'ui/separator.js',
            'ui/mask.js',
            'ui/popup.js',
            'ui/colorpicker.js',
            'ui/tablepicker.js',
            'ui/stateful.js',
            'ui/button.js',
            'ui/splitbutton.js',
            'ui/colorbutton.js',
            'ui/tablebutton.js',
            'ui/autotypesetpicker.js',
            'ui/autotypesetbutton.js',
            'ui/cellalignpicker.js',
            'ui/pastepicker.js',
            'ui/toolbar.js',
            'ui/menu.js',
            'ui/combox.js',
            'ui/dialog.js',
            'ui/menubutton.js',
            'ui/multiMenu.js',
            'ui/shortcutmenu.js',
            'ui/breakline.js',
            'ui/message.js',
            'adapter/editorui.js',
            'adapter/editor.js',
            'adapter/message.js',
            'adapter/autosave.js'
        ],
        dist_files = [
            'ueditor.all.min.js'
        ];

    return BaseObject.extend({
        initialize: function (paramas) {
            var _this = this,
                params = _.extend({config: {}, listener: []}, paramas);

            var URL = _global.static_url + 'vendor/ueditor/';
            if(EDITOR_DEBUG){
                URL = _global.static_url + 'ueditor-src/';
            }
            window.UEDITOR_CONFIG = {
                //为编辑器实例添加一个路径，这个不能被注释
                UEDITOR_HOME_URL: URL, serverUrl: "/assets/index"
                //图片上传配置区
                , imageUrl: _.U({m: 'ueditor', a: 'upload', type: 'image'})             //图片上传提交地址
                , imagePath: _global.upload_url + 'assets/'                    //图片修正地址，引用了fixedImagePath,如有特殊需求，可自行配置
                //涂鸦图片配置区
                , scrawlUrl: URL + "php/scrawlUp.php"           //涂鸦上传地址
                , scrawlPath: URL + "php/"                            //图片修正地址，同imagePath

                //附件上传配置区
                , fileUrl: URL + "php/fileUp.php"               //附件上传提交地址
                , filePath: URL + "php/"                   //附件修正地址，同imagePath
                //,fileFieldName:"upfile"                    //附件提交的表单名，若此处修改，需要在后台对应文件修改对应参数

                //远程抓取配置区
                //,catchRemoteImageEnable:true               //是否开启远程图片抓取,默认开启
                , catcherUrl: URL + "php/getRemoteImage.php"   //处理远程图片抓取的地址
                , catcherPath: URL + "php/"                  //图片修正地址，同imagePath
                //图片在线管理配置区
                //, imageManagerUrl: URL + "php/imageManager.php"       //图片在线管理的处理地址
                , imageManagerUrl: _.U({m: 'ueditor', a: 'index', type: 'image'})       //图片在线管理的处理地址
                , imageManagerPath: _global.upload_url + 'assets/'                                    //图片修正地址，同imagePath

                //屏幕截图配置区
                , snapscreenHost: location.hostname                                 //屏幕截图的server端文件所在的网站地址或者ip，请不要加http://
                , snapscreenServerUrl: URL + "php/imageUp.php" //屏幕截图的server端保存程序，UEditor的范例代码为“URL +"server/upload/php/snapImgUp.php"”
                , snapscreenPath: URL + "php/", snapscreenServerPort: location.port                                   //屏幕截图的server端端口
                //,snapscreenImgAlign: ''                                //截图的图片默认的排版方式

                //word转存配置区
                , wordImageUrl: URL + "php/imageUp.php"             //word转存提交地址
                , wordImagePath: URL + "php/"                       //
                //,wordImageFieldName:"upfile"                     //word转存表单名若此处修改，需要在后台对应文件修改对应参数

                //视频上传配置区
                , getMovieUrl: URL + "php/getMovie.php"                   //视频数据获取地址
                , videoUrl: URL + "php/fileUp.php"               //附件上传提交地址
                , videoPath: URL + "php/"                   //附件修正地址，同imagePath
            };
            if (_.empty(window.ueditor)) {
                if (EDITOR_DEBUG) {
                    _.each(src_files, function (val) {
                        $('body').append('<script type="text/javascript" src="' + _global.static_url + 'ueditor-src/_src/' + val + '"></script>');
                    });
                } else {
                    _.each(dist_files, function (val) {
                        $('body').append('<script type="text/javascript" src="' + _global.static_url + 'vendor/ueditor/' + val + '"></script>');
                    });
                }
            }
            _this.init_ueditor(params);
        },
        init_ueditor: function (params) {
            //params.config.imageCompressEnable=false;
            var editor = window.ueditor = new UE.ui.Editor(params.config);
            _.each(params.listener, function (val, key) {
                editor.addListener(key, val);
            });
            editor.render(params.el);
            editor.ready(params.ready);
            this.instance = editor;
        }
    });
});